package cn.mogugo.joke.tld;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.hibernate.util.StringHelper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.mogugo.comm.PageSupport;
import cn.mogugo.joke.manager.JokeObjManager;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.util.tools.NumberTools;
import cn.mogugo.util.tools.Page;
import cn.mogugo.util.tools.StringTools;

public class JokeObjItemTld extends BodyTagSupport {

	/**
	 * 
	 */
	private Integer pageSize;
	
	private Integer titleLength;
	
	private String jokeObjType;
	
	private Integer savePageStr;
	
	private String orderBy;
	
	
	public int doEndTag() throws JspException {
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletConfig().getServletContext());
		JokeObjManager jokeObjManager = (JokeObjManager) context.getBean("jokeObjManager");		
		List listObj = null;
		PageSupport ps = null;
		Integer pageNo = 1;
		
		
		if(null != jokeObjType && jokeObjType.equals("fromList")){
			jokeObjType = pageContext.getRequest().getParameter("type");
			String strPageNo = pageContext.getRequest().getParameter("pageNo");
			//System.out.println("strPageNo = " + strPageNo);
			if(StringUtils.isNotBlank(strPageNo)){
				pageNo = Integer.valueOf(strPageNo);
			}
		}
		
		
		if(null != jokeObjType && (jokeObjType.equals("hot") || jokeObjType.equals("new"))){
			ps = jokeObjManager.getPage(null, pageNo, pageSize, jokeObjType);
		}else{
			Integer categoryId = 0;
			if(StringUtils.isEmpty(jokeObjType)){
				jokeObjType = pageContext.getRequest().getParameter("type");	
			}
			
			if(null != jokeObjType && NumberTools.isNumber(jokeObjType)){
				categoryId = Integer.valueOf(jokeObjType);
			}else{
				JokeObjDetailTld jokeObjDetailTld = (JokeObjDetailTld) TagSupport.findAncestorWithClass(this, JokeObjDetailTld.class);
				categoryId = jokeObjDetailTld.jo.getJokeCategory().getId();
				jokeObjType = categoryId + "";
			}
			
			Map map = new HashMap();
			map.put("categoryId", categoryId);
			if(StringUtils.isBlank(orderBy)){
				orderBy = "new";
			}
			
			ps = jokeObjManager.getPage(map, pageNo, pageSize, orderBy);
		}
		listObj = ps.getItems();
		
		
		if(savePageStr != null){
			String[] pageStr = null;
			if(savePageStr == 1){
				pageStr = Page.getInstance().createPageNumberStrForJoke7((HttpServletRequest)pageContext.getRequest(),(HttpServletResponse)pageContext.getResponse(), ps);
			}else if(savePageStr == 2){
				pageStr = Page.getInstance().createPageNumberStrForJoke5((HttpServletRequest)pageContext.getRequest(),(HttpServletResponse)pageContext.getResponse(), ps);
			}
			
			
			pageContext.setAttribute("pageStr", pageStr);
		}
		
		for(int i = 0 ; i < listObj.size() ; i ++ ){
			JokeObj jo = (JokeObj)listObj.get(i);
			BodyContent bc = getBodyContent();
			String body = bc.getString();
			String jokeTitle = jo.getJoke_title();
			if(titleLength != null && titleLength > 0 && jokeTitle.length() > titleLength){
				jokeTitle = jokeTitle.substring(0, titleLength)+"..";
			}
			body = StringHelper.replace(body, "$_jo_joke_title" , jokeTitle);
			body = StringHelper.replace(body, "$_jo_full_joke_title" , jo.getJoke_title());
			body = StringHelper.replace(body, "$_jo_joke_id" , jo.getId().toString());
			body = StringHelper.replace(body, "$_jo_click_number" , jo.getClick_number()+"");
			body = StringHelper.replace(body, "$_jo_joke_content" , StringTools.textareaForLine(jo.getJoke_content()));
			body = StringHelper.replace(body, "$_jo_type" , jokeObjType+"");
			
	        try {
	            pageContext.getOut().print(body);
	        } catch (IOException e) {
	        }
		}

		
		return this.EVAL_PAGE;
				
		
	}
	
	

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public Integer getSavePageStr() {
		return savePageStr;
	}

	public void setSavePageStr(Integer savePageStr) {
		this.savePageStr = savePageStr;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTitleLength() {
		return titleLength;
	}

	public void setTitleLength(Integer titleLength) {
		this.titleLength = titleLength;
	}

	public String getJokeObjType() {
		return jokeObjType;
	}

	public void setJokeObjType(String jokeObjType) {
		this.jokeObjType = jokeObjType;
	}
	
	
	
	
}
